// app.js
App({
  server: 'https://www.zhongchengpg.com/user/api',
  request: function({url="", data={}, responseType="json", success, fail, complate, method="POST"}){

    if(url.indexOf('/login/accountPasswordLogin')>-1){
      wx.removeStorageSync('session')
    }
    let cookie = wx.getStorageSync('session')
    wx.request({
      url: this.server + url,
      data: data,
      header: {
        "conrent-Type": "application/json",
        "Cookie": cookie
      },
      method: method,
      success: res=>{
        if(url.indexOf('/login/accountPasswordLogin')>-1&&res.cookies){
          wx.setStorageSync('session',res.cookies[0])
        }
        if(res.statusCode==500){
          this.toast('server error')
        }else if(res.data.code==1){
          // 登录过期清楚cookie
          wx.removeStorageSync('session')
          wx.reLaunch({
            url: '/pages/index/index',
          })
          this.toast(res.data.message)
        }else if(res.data.code==10||res.data.code==6){
          this.toast(res.data.message)
        }else{
          success(res.data)
        }
      },
      fail: res=>{
        wx.showToast({
          icon: 'loading',
          title: '请求超时',
          duration: 2000
        })
      },
      complete: res=>{
        if(complate)
          complate(res);
      }
    })
  },
  toast: function(title){
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 1500,
      mask: false
    })
  },



  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    userInfo: null,
    cookie: ''
  }
})
